package com.study.it.dubbo;


import com.codingapi.tx.annotation.TxTransaction;
import com.study.it.dubbo.face.AccountDubbo;
import com.study.it.dubbo.face.RecordDubbo;
import com.study.it.entity.Account;
import com.study.it.entity.Record;
import com.study.it.hystrix.command.DubboCommand;
import com.study.it.hystrix.command.HyCommand;
import com.study.it.service.AccountService;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service("accountdubbo")
public class AccountDubboImpl implements AccountDubbo {
    @Autowired
    private AccountService service;
    @Autowired
    @Qualifier("recordDubboService")
    private RecordDubbo recordDubboService;

    @Override
    public String say() {
        return "hello";
    }

    @Transactional(readOnly = true)
    @Override
    public List<Account> findList() {
        return service.findList();
    }

    @Transactional(readOnly = true)
    @Override
    public Account findById(Integer id) {
        return service.findOne(id);
    }

    @Transactional(readOnly = true)
    @Override
    public Account findByCardNo(String cardno) {
        DetachedCriteria dc=DetachedCriteria.forClass(Account.class);
        dc.add(Restrictions.eq("cardNo",cardno));
        Account ac=service.findOne(dc);
        return ac;
    }

    @TxTransaction(isStart = true)
    @Transactional
    @Override
    public Account add(String cardno, Float money) {
        try {
            Account ac = new Account();
            ac.setCardNo(cardno);
            ac.setBalance(money);
            ac.setEnable(true);
            service.add(ac);
            DubboCommand<Record> cmd = new DubboCommand<Record>(
                    this.recordDubboService
                    , "add"
                    , new Object[]{money, ac.getId()}
                    , null);
            Record r = cmd.execute();
            if(r==null)
                throw new RuntimeException("远程调用失败");
//        RecordAddCommand cmd =new RecordAddCommand(this.recordDubboService,money,ac.getId());
//        Record r=cmd.execute();
//        if(r==null)
//            System.out.println("record远程调用出错");

//         recordDubboService.add(money, ac.getId());
            return ac;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            throw new RuntimeException(ex);
        }

    }

    @Transactional
    @TxTransaction//配角这样写(不是事务的开端)
    @Override
    public Account editMoney(String cardno, Float money) {
        Account ac= this.findByCardNo(cardno);
        ac.setBalance(ac.getBalance()+money);
        service.edit(ac);
        return ac;
    }

    @Transactional
    @TxTransaction//配角这样写(不是事务的开端)
    @Override
    public Account editMoney(Integer accid, Float money) {
        Account ac= this.findById(accid);
        ac.setBalance(ac.getBalance()+money);
        service.edit(ac);
        return ac;
    }

    @Transactional
    @TxTransaction//配角这样写(不是事务的开端)
    @Override
    public Account editEnable(String cardno, Boolean enable) {
        Account ac= this.findByCardNo(cardno);
        ac.setEnable(false);
        return ac;
    }

    @Transactional
    @TxTransaction//配角这样写(不是事务的开端)
    @Override
    public Integer remove(Integer id) {
        service.remove(id);
        return 1;
    }
}


  /*  //dubbo的远程调用
    Record rr =new HyCommand<Record>(
            recordDubboService
            ,"add"
            ,new Object[]{money,ac.getId()}
            ,null
    ).execute();
        if(rr==null)
                System.out.println("超时");
                else
                System.out.println(rr.getId()+rr.getMoney());*/